package com.xtianzhuang.www.function;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.velocity.VelocityContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.xtianzhuang.www.dao.TdSStaticMapper;
import com.xtianzhuang.www.entity.TdSStatic;
import com.xtianzhuang.www.entity.TdSStaticExample;
import com.xtianzhuang.www.utils.StringUtil;
import com.xtianzhuang.www.utils.VelocityUtil;

@RunWith(SpringJUnit4ClassRunner.class) // 表示继承了SpringJUnit4ClassRunner类
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class GeneratConstants {
	private static Logger logger = Logger.getLogger(RollBackTest.class);
	private static String generatorConfig_vm_base = "C:\\project\\mine\\web2019\\src\\main\\resources\\template\\Constants.vm";
	public static String generatorConfig_XML_base = "C:\\project\\mine\\web2019\\src\\main\\java\\com\\xtianzhuang\\www\\entity\\constants\\";
	@Autowired
	private TdSStaticMapper tdSStaticMapper;

	@Test
	public void GeneratConstants() {
		TdSStaticExample example = new TdSStaticExample();
		example.or().andTypeIdEqualTo("TYPE_ID").andIsDeletedEqualTo("0").andCodeEqualTo("1");
		List<TdSStatic> list = tdSStaticMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			this.StartGeneratConstansts(list);
		}
	}


	public void StartGeneratConstansts(List<TdSStatic> list) {

		for (TdSStatic tdSStatic : list) {
			logger.info("开始构建 : " + tdSStatic.getDataId() + "Constants");
			VelocityContext context = new VelocityContext();
			String generatorConfig_vm = generatorConfig_vm_base;
			TdSStaticExample example2 = new TdSStaticExample();
			example2.or().andTypeIdEqualTo(tdSStatic.getDataId()).andIsDeletedEqualTo("0");
			List<TdSStatic> list2 = tdSStaticMapper.selectByExample(example2);
			for (TdSStatic tdSStatic2 : list2) {
				String code = StringUtil.humpToLine(tdSStatic2.getCode()).toUpperCase();
				System.out.println(code);
				tdSStatic2.setCode(code);
			}
			Map<String, Object> map = new HashMap<String, Object>();
			context.put("list", list2);
			context.put("packageName", "com.xtianzhuang.www.entity.constants;");
			String constantName = com.xtianzhuang.www.utils.StringUtil.toUpperCaseFirstOne(tdSStatic.getDataId())
					+ "Constants";
			String ConstantDescription = tdSStatic.getDataName() + " 常量";
			context.put("ConstantName", constantName
					);
			context.put("ConstantDescription", ConstantDescription);
			try {
				createFile(generatorConfig_XML_base,
						constantName);
				VelocityUtil.generate(generatorConfig_vm, generatorConfig_XML_base + constantName + ".java", context);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void createFile(String filePath, String fileName) {
		String file = filePath + fileName + ".java";
		System.out.println(file);
		File f = new File(file);

		try {
			if (!f.exists()) {
				f.createNewFile();
				System.out.println("create " + fileName + " success");
			} else {
				f.delete();
				System.out.println("delete " + fileName + " success");
				f.createNewFile();
				System.out.println("create " + fileName + " success");
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}